﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;

namespace PivotPhysics_Viewer
{
    /// <summary>
    /// Class to keep track of known collections that we can view
    /// </summary>
    public static class KnownCollections
    {
        /// <summary>
        /// Name of the collection file we will be using to store our stuff in
        /// </summary>
        private const string _collection_filename = "known_collections.txt";

        /// <summary>
        /// Load the collections from isolated storage, or make up a temporary list
        /// </summary>
        /// <returns></returns>
        public static PlotCollectinInfo[] LoadCollectionList()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(_collection_filename))
                {
                    return CreateListFromStrings(new string[] { "recent", DateTime.Now.Year.ToString() });
                }
                else
                {
                    using (var reader = store.OpenFile(_collection_filename, System.IO.FileMode.Open))
                    {
                        var ser = new XmlSerializer(typeof(PlotCollectinInfo[]));
                        var list = ser.Deserialize(reader) as PlotCollectinInfo[];
                        reader.Close();
                        return list;
                    }
                }
            }
        }

        /// <summary>
        /// Given a list of names (that would, for example, come back from a http request), generate
        /// the actual collection list. including adding on the "all" to the top. Keep whatever
        /// order they come in on as well.
        /// </summary>
        public static PlotCollectinInfo[] CreateListFromStrings(string[] collectionNames)
        {
            List<PlotCollectinInfo> list = new List<PlotCollectinInfo>();
            list.Add(new PlotCollectinInfo() { Name = "All", uri = "http://deeptalk.phys.washington.edu/physplots/collection.cxml" });

            foreach (var item in collectionNames)
            {
                list.Add(new PlotCollectinInfo() { Name = item, uri = "http://deeptalk.phys.washington.edu/physplots/" + item + ".cxml" });
            }
            return list.ToArray();
        }

        /// <summary>
        /// Write the list to our local storage so that next time we start up we can quickly grab it
        /// </summary>
        /// <param name="list"></param>
        public static void SaveCollectionList(PlotCollectinInfo[] list)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var writer = store.CreateFile(_collection_filename))
                {
                    var ser = new XmlSerializer(typeof(PlotCollectinInfo[]));
                    ser.Serialize(writer, list);
                    writer.Close();
                }
            }
        }
    }
}
